<?php
// +----------------------------------------------------------------------
// | 分类模型
// +----------------------------------------------------------------------

class category_model extends model {

	public $module; //当前模块
	public $tree = array();
	protected $_lists; //全部分类数组
	protected $cachename = 'model_category_list_all'; //分类列表缓存名称
	public $letter = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'other');

	public function _initialize() {
		
	}

	/*
	 * 得到分类位置导航
	 * @param int $catid 分类ID
	 * @param string $s 分隔符
	 * @param bool $url 是否带超链接
	 * @return return
	 */
	public function getPositionHtml($catid, $sp=' &gt; ', $url=true){
		$rows = $this->getParentList($catid,true);
		$res = array();
		foreach($rows as $v){
			if($url) $v['name'] = "<a href='".url('category',$v['id'])."'>{$v['name']}</a>";
			$res[] = $v['name'];
		}
		return implode($sp,$res);
	}

	//获取字母列表
	public function getLetterList() {
		//字母列表
		$arr = $this->letter;
		$letters = array();
		foreach ($arr as $v) {
			$row = array();
			$row['name'] = ucfirst($v);
			$row['url'] = url('letter', $v);
			$letters[$v] = $row;
		}
		return $letters;
	}

	//删除分类 同时删除下面所有子分类
//	public function categoryDelete($id){
//		//此节点下的所有子节点
//		$sun = $this->tree()->getChildList($id);
//		$ids = array();
//		$ids[] = $id;
//		if ($sun) {
//			foreach ($sun as $v) {
//				$ids[] = $v['id'];
//			}
//		}
//		$ids = implode(',', $ids);
//		$result = $this->where("id in($ids)")->delete();
//		$this->areaCacheDelete();
//		return $result;
//	}


	//设置数据格式
	public function format($lists){
		if(empty($lists)) return array();
		foreach($lists as $k=>$v){
			$v['url'] = url('category',$v);
			$v['img'] = imgurl($v['image_id'],'160_120');
			$lists[$k] = $v;
		}
		return $lists;
	}

	//获取全部分类下拉框
	public function getSelect($ids='', $option='', $default_name='', $rule='') {
		$lists = $this->tree()->getChildList(0);
		$rule = "<option value='\$id'\$selected>\$spacer\$name</option>";
		if (empty($option))
			$option = ' name="data[catid]" id="catid"';
		$str = form::select($ids, $lists, $option, $default_name, $rule);
		return $str;
	}

	// 得到所有未排序的节点
	public function getNodes() {
		return $this->tree()->nodes;
	}

	// 得到某个节点
	public function getNode($id,$field='') {
		$node = $this->tree()->getNode($id);
		return $field ? (isset($node[$field])?$node[$field]:'') : $node;
	}

	// 分类id转换成名称
	public function id2name($id) {
		return $this->getNode($id,'name');
	}

	// 分类id转换成目录
	public function id2dir($id) {
		return $this->getNode($id,'dir');
	}

	// 分类id转换成URL
	public function id2url($id) {
		return $this->getNode($id,'url');
	}

	// 得到父列表
	public function getParentList($id,$own=false) {
		$res = $this->tree()->getParentList($id);
		if($own){
			$res[$id] = $this->getNode($id);
		}
		return $res;
	}

	// 得到子列表
	public function getChildList($id=0) {
		return $this->tree()->getChildList($id);
	}

	// 得到子树
	public function getChildTree($id=0) {
		return $this->tree()->getChildTree($id);
	}

	//得到分类处理类
	public function tree() {
		if (!empty($this->tree)) {
			return $this->tree;
		} else {
			$rows = S($this->cachename);
			if (empty($rows)) {
				$lists = $this->order('sort ASC,pid ASC,id ASC')->findAll();
				$tree = new tree($lists, 'id', 'pid');
				$rows = array();
				foreach ($lists as $k => $v) {
					$rows[$v['id']] = $v;
				}
				S($this->cachename, $rows, 3600 * 24);
			}			
			$this->tree = new tree($rows, 'id', 'pid');
			unset($rows);
			return $this->tree;
		}
	}

	//删除全部分类缓存
	public function cacheDelete() {
		S($this->cachename, null);
		return true;
	}

}